import 'package:dingtalk/mock/mock_chat_message_record.dart';
import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class THTestScreen extends StatefulWidget {
  @override
  _THTestScreenState createState() => _THTestScreenState();
}

class _THTestScreenState extends State<THTestScreen> {

  @override
  void initState() {
    super.initState();

    _initDatabase();
  }

  _initDatabase() async {
    String path = await getDatabasesPath();
    String _databasePath = join(path, 'flutter_dingtalk.db');
    await deleteDatabase(_databasePath);

//    _database = await openDatabase(_databasePath, version: 1,
//        onConfigure: (Database db) async {
////      print("onConfigure ${db.runtimeType}");
//      await db.execute(chatMessageRecordSql);
//    }, onCreate: (Database db, int version) {
//      print("onCreate ${db.runtimeType} $version ");
//    }, onOpen: (Database db) {
//      print("onOpen ${db.runtimeType}");
//    }, onDowngrade: (Database db, int oldVersion, int newVersion) {
//      print("onDowngrade ${db.runtimeType} $oldVersion $newVersion");
//    }, onUpgrade: (Database db, int oldVersion, int newVersion) {
//      print("onUpgrade ${db.runtimeType} $oldVersion $newVersion");
//    });

//    database.(table);
  }

  _addData() async {
    await MockChatMessageRecord.initChatMessageRecord();
  }

  _deleteData() async {}

  _updateData() {}

  _queryData() async {
//    var list = await _database.query('chat_message_record', columns: [
//      'id',
//      'chat_id',
//      "user_id",
//      "user_name",
//      "chat_name",
//      "message",
//      "message_type",
//      "avatar",
//      "time",
//      "un_read_count",
//      "is_single",
//      "is_group",
//      "is_disturbing",
//      "is_stick",
//      "is_special_attention",
//      "is_at_you",
//      "is_at_all"
//    ]);
//    print(list);

//    var list = await _database
//        .query('chat_message_record', where: " id = ? ", whereArgs: [1]);
//    print(list);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("SQFLITE"),
        ),
        body: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.symmetric(horizontal: 10),
            child: Column(
              children: [
                Wrap(
                  crossAxisAlignment: WrapCrossAlignment.end,
                  spacing: 10,
                  children: [
                    RaisedButton(child: Text('增'), onPressed: _addData),
                    RaisedButton(child: Text('删'), onPressed: _deleteData),
                    RaisedButton(child: Text('改'), onPressed: _updateData),
                    RaisedButton(child: Text('查'), onPressed: _queryData),
                  ],
                ),
              ],
            ),
          ),
        ));
  }
}
